package com.lxz.jucdemo.functional_Interface;

import java.util.ArrayList;
import java.util.stream.Collectors;

/**
 * @Author: 刘新智
 * @Date: 2025/10/27 11:37
 * @Description: stream流式计算
 * @Version: v1.0
 */
public class StreamTest {
    public static void main(String[] args) {
        ArrayList<User> users = new ArrayList<>();
        users.add(new User("张三",18,"男"));
        users.add(new User("李四",19,"女"));
        users.add(new User("王五",20,"男"));
        // 获取名称
        System.out.println(users.stream()
                .filter(user -> {
                    return user.getAge() > 18; // 过滤年龄大于18
                })
                .map(User::getName)
                .limit(2).collect(Collectors.toList()));
    }
}
class User{
    private String name;
    private Integer age;
    private String sex;
    public User(String name,Integer age,String sex){
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}
